/**
 * 
 */
package com.archerdu.apply.service.impl;

import com.archerdu.apply.dao.AdvertiseCatalogDAO;
import com.archerdu.apply.dao.AdvertiseDAO;
import com.archerdu.apply.model.Advertise;
import com.archerdu.apply.model.AdvertiseCatalog;
import com.archerdu.apply.service.AdvertiseManager;
import com.archerdu.base.dao.IBaseDao;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.base.service.BaseManager;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;

/**
 * @author archer
 * @version 1.0.0
 */
public class AdvertiseManagerImpl extends BaseManager<Advertise, String> implements
        AdvertiseManager {

    private static final long serialVersionUID = 8596375322783640728L;

    private AdvertiseDAO advertiseDao;
    private AdvertiseCatalogDAO    advertiseCatalogDao;

    @Override
    public void add(Advertise entity) throws Exception {
        // 目录不存在
        if(null == entity.getCatalogId()){
            throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
        }
        
        // 目录不存在
        AdvertiseCatalog cate = advertiseCatalogDao.get(entity.getCatalogId());
        if( null == cate){
            throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
        }
        
        // 非广告位目录不能添加广告
        if(!cate.getType().equals(DataConstant.ADVERTIES_CATE_TYPE_POSITION)){
            throw new BussinessException(ErrorConstant.ADVERTISE_NOT_POSITION_CATE);
        }
        entity.setCreatetime(getSqlDate());
        
        super.add(entity);
    }

    public void setAdvertiseDao(AdvertiseDAO dao) {
        advertiseDao = dao;
    }

    public AdvertiseDAO getAdvertiseDao() {
        return advertiseDao;
    }

    @Override
    protected IBaseDao<Advertise, String> getDao() {
        return (IBaseDao<Advertise, String>) advertiseDao;
    }

    public AdvertiseCatalogDAO getAdvertiseCatalogDao() {
        return advertiseCatalogDao;
    }

    public void setAdvertiseCatalogDao(AdvertiseCatalogDAO advertiseCatalogDao) {
        this.advertiseCatalogDao = advertiseCatalogDao;
    }
}
